#include "utility.h"



int mW_blit(BITMAP * source, BITMAP * destination, int dest_x, int dest_y)
{
	
	int source_x = 0;
	int source_y = 0;
	
	int width = source->w;
	int height = source->h;
	
	// null source or destination
	if (source == NULL)
		mW_error("mW_blit was passed a null source bitmap");
	if (destination == NULL)
		mW_error("mW_blit was passed a null destination bitmap");
	
	// way too far right or down
	if ((dest_x >= destination->w) || (dest_y >= destination->h))
		return 0;

	// too far left
	if (dest_x < 0)
	{
		source_x -= dest_x;
		width += dest_x;
		dest_x = 0;
	}
	// too far right
	else if (dest_x + source->w > destination->w)
	{
		width = destination->w - dest_x;
	}
	// too far up
	if (dest_y < 0)
	{
		source_y -= dest_y;
		height += dest_y;
		dest_y = 0;
	}
	// too far down
	else if (dest_y + source->h > destination->h)
	{
		height = destination->h - dest_y;
	}


	if ((width <= 0) || (height <= 0))
		return 0;
		
	acquire_bitmap(destination);
	masked_blit(source, destination, source_x, source_y, dest_x, dest_y, width, height);
	release_bitmap(destination);
	
	return 1;
	
}



/*
 * Does validation on a number received from a string, finding a number
 *   from string[0] until 0x00 or ' '
 * Returns an int between 0 and 2^16 on success
 * Returns -1 on failure
 */

int mW_get_unsigned_short_from_string (const char * string)
{

	int i;
	char tmp[7];
	
	memset(tmp, 0, 7);

	// let's do some validation 
	for (i = 0; i < 6; i++)
	{
		// we reach an end delimiter
		if ((string[i] == ' ') || (string[i] == 0))
		{
			memcpy(tmp, string, i);
			break;
		}
		// a character is not a number
		else if ((string[i] < '0') || (string[i] > '9'))
			return -1;
	}
	
	// we have 6 digits
	if (i == 5)
		return -1;

	i = atoi(tmp);
	
	if (i < 65536)
		return i;
	else
		return -1;
		
}



// takes text and breaks it into multiple lines to fit in certain dimensions
//   then puts that text into a bitmap
BITMAP * mW_wordwrap_text (const char * text, int width, int height, int color)
{
	
	int text_i;
	int line_i;
	int line_n;
	int i;
	
	BITMAP * textbox;
	char line[256];
	char next_line[256];
	
	
	if (strlen(text) == 0)
	{
		printf("wtf");
		return NULL;
	}
	
	textbox = create_bitmap(width, height);
	rectfill(textbox, 0, 0, width - 1, height - 1, makecol(0xff, 0x0, 0xff));
	
	
	line_i = 0;
	memset(&line, 0, 256);
	
	line_n = 0;
	
	for (text_i = 0; text_i <= strlen(text); text_i++)
	{
		line[line_i] = text[text_i];
		if (text_length(font, line) > width)
		{
			// if the last character is a space
			if (line[line_i] == ' ')
			{
				
				// just get rid of the space
				line[line_i] = 0;
				
				// draw line
				acquire_bitmap(textbox);
				textout_ex(textbox, font, line, 0, (text_height(font) + 2) * line_n++, color, -1);
				release_bitmap(textbox);
				
				// clear line and reset line_i
				memset(&line, 0, 256);
				line_i = 0;
				
			}
			// if the last character is not a space (and part of a word)
			else
			{
				
				// copy over whatever is in last word to next_line
				memset(next_line, 0, 256);
				for (i = line_i; i >= 0; i--)
				{
					if (line[i] == ' ')
					{
						strcpy(next_line, &line[i + 1]);
						line[i] = 0;
						break;
					}
				}
				// if we didn't copy anything over, just take the last character
				//   of line and put it in next_line
				if (strlen(next_line) == 0)
				{
					next_line[0] = line[line_i];
					line[line_i] = 0;
				}
				
				// draw line				
				acquire_bitmap(textbox);
				textout_ex(textbox, font, line, 0, (text_height(font) + 2) * line_n++, color, -1);
				release_bitmap(textbox);
				
				// clear line
				memset(line, 0, 256);
				
				// copy next_line in to line
				strcpy(line, next_line);
				
				// reset line_i
				line_i = strlen(line);
				
			}
			
			// check line_n to see if that was the last line
			if ((text_height(font) + 2) * (line_n + 1) > height)
				return textbox;
				
		}
		else
			line_i++;
	}
	
	acquire_bitmap(textbox);
	textout_ex(textbox, font, line, 0, (text_height(font) + 2) * line_n, color, -1);
	release_bitmap(textbox);
	
	return textbox;
	
}
			
		
